﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JCTimes.DomainModel.Entity;
using JCTimes.DomainModel.Repository;

namespace JCTimes.EFRepository.RepositoryImpl {
    public class FunctionRoleRepository : BaseRepository<FunctionRole>, IFunctionRoleRepository {
        public bool IsAllowed(string url, string roleID, string userID) {
            var query = from fr in db.FunctionRoles
                        from f in db.Functions
                        where fr.FunctionID == f.ID
                        where f.URL == url
                        where fr.RoleID == roleID || fr.UsersID == userID
                        select fr;
            return query != null && query.Count() > 0;
        }

        public IEnumerable<Function> FunctionsByRole(string roleID) {
            if (roleID==null) {
                return db.Functions;
            } else {
                var query = from fr in db.FunctionRoles
                            from f in db.Functions
                            where fr.FunctionID == f.ID
                            select f;
                return query.Distinct();
                    
            }
        }
    }
}
